<?php
namespace Hilaoyu\Utils;

/**
 * Created by laoyu.
 * User: hilaoyu@qq.com
 * Date: 2018/7/12
 * Time: 下午9:33
 */



class UtilCli
{
    static protected $logCallback=null;

    static public function registerLogCallback(\Closure $callback){
        static::$logCallback = $callback;
    }

    static public function nullDrive(){
        if(UtilSupport::runningInWindows()){
            return 'nul';
        }

        return '/dev/null';
    }

    /**
     * 运行命令
     * @param $command
     * @param null $output
     * @param null $output_redirect
     * @return mixed
     */
    static public function execute($command,&$output=null,$output_redirect=null,$output_glue='; '){
        $command = static::buildCommand($command,$output,$output_redirect);

        $output_redirect && file_put_contents($output_redirect,$command . PHP_EOL,FILE_APPEND);

        $locale = 'zh_CN.UTF-8';
        setlocale(LC_ALL, $locale);
        putenv('LC_ALL='.$locale);

        //dump($command);
        @exec($command,$output,$status);
        //dump($output,$status);
        if($output){
            $output = UtilArr::toString($output,$output_glue,false);
        }
        //dump($output);
        UtilSupport::tryQuiet(function () use ($command,$status,$output){
           call_user_func(static::$logCallback,[
               'command' =>$command,
               'status' =>$status,
               'output' =>$output,
           ],(0 !== $status));
        });

        return 0 === $status;
    }

    /**
     * @param $command
     * @param null $output
     * @param null $output_redirect
     * @return string
     */
    static public function buildCommand($command,$output=null,$output_redirect=null){
        $command = trim($command);
        if($output_redirect && is_string($output_redirect)){
            //$output_redirect .= '.log';
        }else{
            $output_redirect = static::nullDrive();
        }

        if(is_null($output)){
            if(('nohup' != substr($command,0,5))){
                $command = 'nohup ' .$command;
            }

            $command = rtrim($command,'&');


            if(('&' != substr($command,-1))){
                $command .= ' >>'. $output_redirect .' 2>&1 &'; //2>&1
            }

        }

        return $command;
    }

    /**
     * 命令是否已在运行
     * @param $command   命令行
     * @param int $threads 线程数
     * @return bool
     */

    static public function hasRunning($command,$threads = 1){
        $base_cmd = "ps -ef ";
        if('Linux' == PHP_OS){
            $base_cmd .= ' --cols 1000';
        }
        $cmd = $base_cmd ."|grep '".escapeshellcmd($command)."' | grep -v '/bash -c' | grep -v grep ";
        $cmd_array = [];
        $re = exec($cmd,$cmd_array);
//        dump($cmd,count($cmd_array));exit;
//        file_put_contents(storage_path("aa.txt"), $cmd .'-->'. json_encode($cmd_array) ."\n", FILE_APPEND);
        if (count($cmd_array) >= $threads) {
            return true;
        }
        return false;
    }
}



